﻿using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Growatt.ShinebusService.Service
{
    public class CustomHealthCheck : IHealthCheck
    {
        public async Task<HealthCheckResult> CheckHealthAsync(
            HealthCheckContext context,
            CancellationToken cancellationToken = default)
        {
            try
            {
                // 执行健康检查逻辑
                var isHealthy = await CheckSomeExternalServiceAsync();

                if (isHealthy)
                {
                    return HealthCheckResult.Healthy("The custom check is healthy.");
                }
                else
                {
                    return HealthCheckResult.Unhealthy("The custom check is unhealthy.");
                }
            }
            catch (Exception ex)
            {
                return new HealthCheckResult(
                    status: HealthStatus.Unhealthy,
                    description: "The custom check failed.",
                    exception: ex,
                    data: null);
            }
        }

        private async Task<bool> CheckSomeExternalServiceAsync()
        {
            // 实现你的健康检查逻辑
            return true; // 假设总是健康的
        }
    }
}
